/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.itintegration.catalogue.camel.processor;

import java.io.IOException;
import java.util.List;
import org.apache.camel.Exchange;
import org.apache.camel.Property;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import se.sll.itintegration.catalogue.gui.data.IdmapView;
import se.sll.service.catalogue.common.rest.response.VMIdMapResponse;

/**
 *
 * @author khaleddaham
 */
public class IdmapProcessor {

    /**
     *
     * @param exchange
     */
    public void createView(Exchange exchange) {
        exchange.setProperty("view", new IdmapView());
    }

    /**
     *
     * @param exchange
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public void idmapResponseJSON(Exchange exchange) throws JsonParseException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        List<VMIdMapResponse> responses = mapper.readValue(exchange.getIn().getBody(String.class), new TypeReference<List<VMIdMapResponse>>() {
        });
        exchange.getIn().setBody(responses);
    }

    public void convertToIdmapView(Exchange exchange, @Property("view") IdmapView view) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        List<VMIdMapResponse> response = mapper.readValue(exchange.getIn().getBody(String.class), new TypeReference<List<VMIdMapResponse>>() {
        });
        view.setIdmap(response);
        exchange.getIn().setBody(view);
    }
}
